/*
 * ReportsDialog.java
 *
 * Created on 29 November 2006, 10:19
 */

package org.foment.gem.dialogs;

import java.awt.Component;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.sql.SQLException;
import javax.swing.DefaultComboBoxModel;
import static org.foment.joliage3.core.internationalization.Internationalization.i18n;
import org.foment.joliage3.reports.*;

/**
 *
 * @author  ivan
 */
public class ReportPrepareDialog extends GemInternalDialog {
  
    private Report _report;
    private Object _extraData;
    
    
    
    /** Creates new form ReportsDialog */
    public ReportPrepareDialog(int id, java.util.HashMap params) throws SQLException {
      Report.initializeReports();
      
      _report = new Report(id, params);
      
      initAll();
    }
    
    /** Creates new form ReportsDialog */
    public ReportPrepareDialog(Report report) {
      Report.initializeReports();
      
      _report = report;
      initAll();
    }
    
    private void initAll() {
      initComponents();
      
      setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
      
      if (_report.getPaper() != null) {
        pnlPaper.add(_report.getPaper(), java.awt.BorderLayout.CENTER);
      } else {
        javax.swing.JLabel lbl = new javax.swing.JLabel(i18n("There are no editable fields in this report."));
        lbl.setHorizontalAlignment(lbl.CENTER);
        pnlPaper.add(lbl, java.awt.BorderLayout.CENTER);
      }
      
      remove(pnlCommands);
      
      txtDocumentTitle.setText(_report.getTitle());
      txtDocumentDescription.setText(_report.getDescription());
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
  // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
  private void initComponents() {
    pnlPaper = new javax.swing.JPanel();
    pnlCommands = new javax.swing.JPanel();
    btnPrint = new javax.swing.JLabel();
    btnClose = new javax.swing.JLabel();
    checkArchive = new javax.swing.JCheckBox();
    pnlArchiveOptions = new javax.swing.JPanel();
    lblDocumentTitle = new javax.swing.JLabel();
    txtDocumentTitle = new javax.swing.JTextField();
    lblDocumentDescription = new javax.swing.JLabel();
    scrolDocumentDescription = new javax.swing.JScrollPane();
    txtDocumentDescription = new javax.swing.JTextArea();
    checkCloseAfterPrint = new javax.swing.JCheckBox();

    setClosable(true);
    setTitle(i18n("Report"));
    pnlPaper.setLayout(new java.awt.BorderLayout());

    getContentPane().add(pnlPaper, java.awt.BorderLayout.CENTER);

    btnPrint.setFont(new java.awt.Font("Dialog", 0, 12));
    btnPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/foment/gem/images/print_print.png")));
    btnPrint.setText(i18n("<html><b>Print</b><br>Print this report</html>"));
    btnPrint.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        btnPrintMouseClicked(evt);
      }
    });

    btnClose.setBackground(new java.awt.Color(102, 204, 255));
    btnClose.setFont(new java.awt.Font("Dialog", 0, 12));
    btnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/foment/gem/images/print_close.png")));
    btnClose.setText(i18n("<html><b>Close</b><br>Close this report</html>"));
    btnClose.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(java.awt.event.MouseEvent evt) {
        btnCloseMouseClicked(evt);
      }
    });

    checkArchive.setSelected(true);
    checkArchive.setText(i18n("Save to archive"));
    checkArchive.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
    checkArchive.setMargin(new java.awt.Insets(0, 0, 0, 0));
    checkArchive.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(java.awt.event.ItemEvent evt) {
        checkArchiveItemStateChanged(evt);
      }
    });

    lblDocumentTitle.setText(i18n("Report title") + ":");

    lblDocumentDescription.setText(i18n("Report description") + ":");

    scrolDocumentDescription.setBackground(new java.awt.Color(255, 255, 255));
    scrolDocumentDescription.setBorder(txtDocumentTitle.getBorder());
    txtDocumentDescription.setColumns(20);
    txtDocumentDescription.setLineWrap(true);
    txtDocumentDescription.setRows(5);
    txtDocumentDescription.setWrapStyleWord(true);
    scrolDocumentDescription.setViewportView(txtDocumentDescription);

    org.jdesktop.layout.GroupLayout pnlArchiveOptionsLayout = new org.jdesktop.layout.GroupLayout(pnlArchiveOptions);
    pnlArchiveOptions.setLayout(pnlArchiveOptionsLayout);
    pnlArchiveOptionsLayout.setHorizontalGroup(
      pnlArchiveOptionsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(lblDocumentTitle, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
      .add(org.jdesktop.layout.GroupLayout.TRAILING, txtDocumentTitle, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
      .add(lblDocumentDescription, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
      .add(scrolDocumentDescription, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
    );
    pnlArchiveOptionsLayout.setVerticalGroup(
      pnlArchiveOptionsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(pnlArchiveOptionsLayout.createSequentialGroup()
        .add(lblDocumentTitle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 15, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(txtDocumentTitle, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(lblDocumentDescription)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        .add(scrolDocumentDescription, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 147, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
    );

    checkCloseAfterPrint.setSelected(true);
    checkCloseAfterPrint.setText(i18n("Close after printing"));
    checkCloseAfterPrint.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
    checkCloseAfterPrint.setMargin(new java.awt.Insets(0, 0, 0, 0));

    org.jdesktop.layout.GroupLayout pnlCommandsLayout = new org.jdesktop.layout.GroupLayout(pnlCommands);
    pnlCommands.setLayout(pnlCommandsLayout);
    pnlCommandsLayout.setHorizontalGroup(
      pnlCommandsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(btnClose, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
      .add(btnPrint, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
      .add(checkArchive, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
      .add(pnlArchiveOptions, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      .add(checkCloseAfterPrint, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE)
    );
    pnlCommandsLayout.setVerticalGroup(
      pnlCommandsLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
      .add(pnlCommandsLayout.createSequentialGroup()
        .add(btnClose, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 53, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(btnPrint, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 53, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(checkArchive)
        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
        .add(pnlArchiveOptions, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
        .add(16, 16, 16)
        .add(checkCloseAfterPrint)
        .addContainerGap(82, Short.MAX_VALUE))
    );
    getContentPane().add(pnlCommands, java.awt.BorderLayout.WEST);

  }// </editor-fold>//GEN-END:initComponents

  private void checkArchiveItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_checkArchiveItemStateChanged
    pnlArchiveOptions.setVisible(evt.getStateChange() == evt.SELECTED);
  }//GEN-LAST:event_checkArchiveItemStateChanged

  private void btnPrintMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnPrintMouseClicked
    if (doPrint() && checkCloseAfterPrint.isSelected()) doClose();
  }//GEN-LAST:event_btnPrintMouseClicked

  private void btnCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCloseMouseClicked
    doClose();
  }//GEN-LAST:event_btnCloseMouseClicked
    
  private boolean doClose() {
    this.setVisible(false);
    try {
      this.setClosed(true);
    } catch (PropertyVetoException ex) {
      ex.printStackTrace();
    }
    return true;
  }    
    
  private boolean doPrint() {
    String oldTitle = getReport().getTitle();
    String oldDesc = getReport().getDescription();
 
    getReport().setTitle(txtDocumentTitle.getText());
    getReport().setDescription(txtDocumentDescription.getText());
    getReport().print(checkArchive.isSelected());
    
    getReport().setTitle(oldTitle);
    getReport().setDescription(oldDesc);
    
    return true;
  }
    
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JLabel btnClose;
  private javax.swing.JLabel btnPrint;
  private javax.swing.JCheckBox checkArchive;
  private javax.swing.JCheckBox checkCloseAfterPrint;
  private javax.swing.JLabel lblDocumentDescription;
  private javax.swing.JLabel lblDocumentTitle;
  private javax.swing.JPanel pnlArchiveOptions;
  private javax.swing.JPanel pnlCommands;
  private javax.swing.JPanel pnlPaper;
  private javax.swing.JScrollPane scrolDocumentDescription;
  private javax.swing.JTextArea txtDocumentDescription;
  private javax.swing.JTextField txtDocumentTitle;
  // End of variables declaration//GEN-END:variables

    
  private java.util.Vector < Report > _reports = new java.util.Vector();

  public Component getNavigatorComponent() {
    return pnlCommands;
  }

  public Report getReport() {
    return _report;
  }

  public Object getExtraData() {
    return _extraData;
  }

  public void setExtraData(Object extraData) {
    this._extraData = extraData;
  }

}
